//
//  EFBChartDataBaseUtil.m
//  Pods
//
//  Created by E.F.B on 15-5-26.
//
//

#import "EFBChartDataBaseUtil.h"
#import "ChartContext.h"

@implementation EFBChartDataBaseUtil

static FMDatabaseQueue* chartdbQueue = nil;
static FMDatabaseQueue *airportDBQueue = nil;

+(NSString *)chartdbFilePath:(NSString *)dbName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *dbPath = [path stringByAppendingPathComponent:dbName];
    
    return dbPath;
}

+(FMDatabaseQueue *)createOrFindDB
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *dbName = [[ChartContext instance] objectForKey:kChartDBFileName];
        chartdbQueue = [[FMDatabaseQueue alloc] initWithPath:[self chartdbFilePath:dbName]];
    });
    return chartdbQueue;
}

+(FMDatabaseQueue *)getChartDatabase
{
    return [self createOrFindDB];
}

+(FMDatabaseQueue *)getAirPortDatabase
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        airportDBQueue = [[FMDatabaseQueue alloc] initWithPath:[self chartdbFilePath:@"airPorts.sqlite"]];
    });
    return airportDBQueue;
}

@end
